package com.icez.base_util_library

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.Exception

/**
 * @author icez
 *  资源文件工具类
 */
class ResourcesUtil {
    companion object{

        val LAYOUTANDROIDATTR = "http://schemas.android.com/apk/res/android"

        val instance:ResourcesUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ResourcesUtil()
        }

    }

    /**
     * @author icez
     *  类型枚举
     */
    enum class DefType{
        ID,STRING,ANIM,MIPMAP,DRAWABLE,DIMEN,ATTR,BOOL
    }

    /**
     *  通过名字获取资源文件ID
     * @param context 上下文
     * @param name 资源名字
     * @param defType [DefType] 支持的类型
     * @param defPackage 包名
     */
    public fun getResourcesId(context: Context, name:String,defType:DefType,defPackage:String = ""):Int{
        var defTypeStr = ""
        when(defType){
            DefType.ID -> {
                defTypeStr = "id"
            }
            DefType.ANIM -> {
                defTypeStr = "anim"
            }
            DefType.DRAWABLE -> {
                defTypeStr = "drawable"
            }
            DefType.MIPMAP -> {
                defTypeStr = "mipmap"
            }
            DefType.STRING -> {
                defTypeStr = "string"
            }
            DefType.DIMEN -> {
                defTypeStr = "dimen"
            }
            DefType.ATTR -> {
                defTypeStr = "attr"
            }
            DefType.BOOL -> {
                defTypeStr = "bool"
            }
        }
        if(defTypeStr.isNullOrEmpty()){
            return 0
        }
        var res = 0

        if(defPackage.isNullOrEmpty()){
            res = context.resources.getIdentifier(name, defTypeStr, context.packageName)
        }else{
            res = context.resources.getIdentifier(name, defTypeStr, defPackage)
        }
        return res
    }

    /**
     *  获取布局中的指定android中指定属性的值；例如：获取布局android:layout_height的值，getLayoutValue(attrs,‘layout_height’)
     * @param attrs 当前布局属性的AttributeSet
     * @param attribute 当前布局的属性名字；比如：layout_height
     */
    public fun getLayoutValue(attrs:AttributeSet?,attribute:String):String?{
        return attrs?.getAttributeValue(LAYOUTANDROIDATTR,attribute)
    }

    /**
     *  获取布局中的指定android中指定属性的值；例如：获取布局android:layout_height的值，getLayoutValue(attrs,‘layout_height’)
     * @param attrs 当前布局属性的AttributeSet
     * @param attribute 当前布局的属性名字；比如：layout_height
     * @param defaultValue 默认的值,如果是字符串，就不会出现返回这个默认值
     */
    public fun <T> getLayoutSpecificValue(attrs:AttributeSet?,attribute:String,defaultValue:T): T?{
        when(defaultValue){
            defaultValue is Boolean -> {
                return attrs?.getAttributeBooleanValue(LAYOUTANDROIDATTR,attribute,defaultValue as Boolean) as T
            }
            defaultValue is Int -> {
                return attrs?.getAttributeIntValue(LAYOUTANDROIDATTR,attribute,defaultValue as Int) as T
            }
            defaultValue is Float -> {
                return attrs?.getAttributeFloatValue(LAYOUTANDROIDATTR,attribute,defaultValue as Float) as T
            }
            defaultValue is String -> {
                return getLayoutValue(attrs, attribute) as T
            }
            else -> {
                throw RuntimeException("暂时不支持您要获取的属性值")
            }
        }

    }

    /**
     *  获取raw文件下面的文件内容
     * @param context 上下文
     * @param rawId 资源id
     */
    public fun getRawFileContent(context:Context?,rawId:Int):String?{
        try{
            val inputStream: InputStream? = context?.resources?.openRawResource(rawId)
            val inputStreamReader: InputStreamReader? = InputStreamReader(inputStream, "utf-8")
            val bufferedReader: BufferedReader? = BufferedReader(inputStreamReader)
            var read: String? = ""
            val stringBuilder = StringBuilder("")
            while (bufferedReader?.readLine().also { read = it } != null) {
                stringBuilder.append(read + "\n")
            }
            inputStream?.close()
            return stringBuilder.toString()
        }catch (ex:Exception){
            ex.printStackTrace()
        }
        return null
    }
}